#!/usr/bin/env python
# Copyright (C) 2007 by Xyhthyx <xyhthyx@gmail.com>

# This file is part of Parcelle.

# Parcelle is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.

# Parcelle is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import os
import gtk
import common

# Dialog window to control the preferences
class Preferences:
	
	def __init__(self, parent, widgets):
		
		# Get parent, widgets and current options
		self.parent = parent
		self.widgets = widgets
		self.options = self.parent.options
		
		# Make widgets reflect the current options
		if os.path.exists(common.START_FILE):
			self.widgets.chkStartup.set_active(True)
		self.widgets.chkAppend.set_active(self.options['append'])
		self.widgets.chkWarning.set_active(self.options['warning'])
		self.widgets.chkBrowse.set_active(self.options['browse'])
		self.widgets.chkHyperlinks.set_active(self.options['hyperlinks'])
		self.widgets.spnRemember.set_value(float(self.options['remember']))
		self.widgets.spnRecent.set_value(float(self.parent.tray.recent.size))
		if self.options['capture'] == 'Primary':
			self.parent.clipboard = gtk.clipboard_get(gtk.gdk.SELECTION_PRIMARY)
			self.widgets.rdoPrimary.set_active(True)
	
	# Update the changes to the options
	def update(self):
		
		# Recent option
		self.widgets.spnRecent.update()
		self.parent.tray.recent.set_size(int(self.widgets.spnRecent.get_value()))
		
		# History option
		self.widgets.spnRemember.update()
		self.options['remember'] = self.widgets.spnRemember.get_value()
		
		# Handle which clipboard
		if self.widgets.rdoClipboard.get_active():
			self.options['capture'] = 'Clipboard'
			self.parent.clipboard = gtk.clipboard_get(gtk.gdk.SELECTION_CLIPBOARD)
			if not self.parent.clipboard_text:
				self.parent.clipboard.set_text('')
			else:
				self.parent.clipboard.set_text(self.parent.clipboard_text)
		else:
			self.options['capture'] = 'Primary'
			self.parent.clipboard = gtk.clipboard_get(gtk.gdk.SELECTION_PRIMARY)
			if not self.parent.clipboard_text:
				self.parent.clipboard.set_text('')
			else:
				self.parent.clipboard.set_text(self.parent.clipboard_text)
		
		# Startup option
		if self.widgets.chkStartup.get_active():
			startupfile = file(common.START_FILE, 'w')
			startupfile.write('Parcelle is set to launch on startup.')
			startupfile.close()
		else:
			if os.path.exists(common.START_FILE):
				os.remove(common.START_FILE)
		
		# Append option
		if self.widgets.chkAppend.get_active():
			self.options['append'] = True
		else:
			self.options['append'] = False
		
		# Warning option
		if self.widgets.chkWarning.get_active():
			self.options['warning'] = True
		else:
			self.options['warning'] = False
		
		# Launch hyperlinks option
		if self.widgets.chkBrowse.get_active():
			self.options['browse'] = True
		else:
			self.options['browse'] = False
		
		# Capture hyperlinks only option
		if self.widgets.chkHyperlinks.get_active():
			self.options['hyperlinks'] = True
		else:
			self.options['hyperlinks'] = False
		
	# Run the dialog and return the new options
	def run(self):
		
		result = self.widgets.frmPreferences.run()
		self.update()
		self.widgets.frmPreferences.destroy()
		if result == gtk.RESPONSE_ACCEPT:
			return self.options
		else:
			return None
